package com.example.administrator.im.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.example.administrator.im.MyApplication;

import java.io.File;

import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * 访问服务器工具类
 * create:2018/10/8 11:02
 * author: 姚鸿杰
 **/
public class HttpUtils {

    /**
     * 返回码
     */
    public static final int SUCCESS = 200;
    public static final int SUCCESS_NO_CONTENT = 204;


    /**
     * 调用url
     */
    //端口
    public static final String PORT = "http://134.175.148.244:8080/";
    //注册链接
    private static final String SIGNUP = PORT + "signup/";
    //获取用户
    private static final String GET_USER = PORT + "get_user/";
    //获取好友
    private static final String GET_FRIEND = PORT + "get_friend/";
    //更新用户数据链接
    private static final String UPDATE_USER = PORT + "update_user/";

    //添加分组链接
    private static final String ADD_GROUP = PORT + "add_group/";
    //获取分组
    private static final String GET_GROUPS = PORT + "get_groups/";
    //分组重命名
    private static final String RENAME_GROUP = PORT + "rename_group/";
    //删除分组链接
    private static final String DELETE_GROUP = PORT + "delete_group/";
    //移动分组链接
    private static final String MOVE_GROUP = PORT + "move_group/";

    //上传图片链接
    private static final String UPLOAD_IMAGES = PORT + "images/";
    private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

    //添加好友
    private static final String ADD_FRIEND = PORT + "add_friend/";
    //删除好友
    private static final String DELETE_FRIEND = PORT + "delete_friend/";
    //获取好友
    private static final String GET_FRIENDS = PORT + "get_friends/";
    //编辑好友
    private static final String EDITING_FRIEND = PORT + "editing_friend/";

    /**
     * 用户更新内容
     */
    public static final String UPDATE_PASSWORD = "password";
    public static final String UPDATE_IMG_PATH = "img_path";
    public static final String UPDATE_NICKNAME = "nickname";
    public static final String UPDATE_SIGNATURE = "signature";
    public static final String UPDATE_SEX = "sex";
    public static final String UPDATE_EMAIL = "email";

    /**
     * 好友编辑内容
     */
    public static final String FRIEND_GROUP = "group";
    public static final String FRIEND_REMARKS = "remarks";
    public static final String FRIEND_DISTURB = "disturb";
    public static final String FRIEND_IMG_PATH = "img_path";


    /**
     * 发送http请求
     *
     * @param callback 回调机制
     */
    public static void sendOkHttpRequest(okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(PORT).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * 注册
     *
     * @param username 账号
     * @param password 密码
     * @param callback 回调接口
     */
    public static void signupRequest(String username, String password, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("username", username)
                .add("password", password)
                .build();

        Request request = new Request.Builder()
                .url(SIGNUP)
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(callback);
    }


    /**
     * 获取用户数据
     *
     * @param callback 回调接口
     */
    public static void getUserRequest(okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("username", getUsername())
                .build();
        Request request = new Request.Builder()
                .url(GET_USER)
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * 获取好友
     *
     * @param friend_id 好友id
     * @param callback
     */
    public static void getFriendRequest(String friend_id, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("username", friend_id)
                .build();
        Request request = new Request.Builder()
                .url(GET_FRIEND)
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * 更新用户数据
     *
     * @param key      更新的字段
     * @param update   更新内容
     * @param callback 接口回调
     */
    public static void updateUserRequest(String key, String update, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("username", getUsername())
                .add(key, update)
                .build();

        Request request = new Request.Builder()
                .url(UPDATE_USER)
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(callback);
    }

    /**
     * 添加分组
     *
     * @param position 分组的位置
     * @param name     分组名字
     * @param callback 接口回调
     */
    public static void addGroupRequest(int position, String name, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("username", getUsername())
                .add("position", String.valueOf(position))
                .add("name", name)
                .build();

        Request request = new Request.Builder()
                .url(ADD_GROUP)
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(callback);
    }

    /**
     * 获取分组
     *
     * @param callback 接口回调
     */
    public static void getGetGroupsRequest(okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("username", getUsername())
                .build();

        Request request = new Request.Builder()
                .url(GET_GROUPS)
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(callback);
    }

    /**
     * 分组重命名
     *
     * @param position 分组位置
     * @param name     分组名字
     * @param callback 接口回调
     */
    public static void renameGroupRequest(int position, String name, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("username", getUsername())
                .add("position", String.valueOf(position))
                .add("name", name)
                .build();

        Request request = new Request.Builder()
                .url(RENAME_GROUP)
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(callback);
    }

    /**
     * 移动分组
     *
     * @param before   移动前的位置
     * @param after    移动后的位置
     * @param callback 接口回调
     */
    public static void moveGroupRequest(int before, int after, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("username", getUsername())
                .add("before", String.valueOf(before))
                .add("after", String.valueOf(after))
                .build();

        Request request = new Request.Builder()
                .url(MOVE_GROUP)
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(callback);
    }

    /**
     * 删除分组
     *
     * @param position 删除的位置
     * @param callback 接口回调
     */
    public static void deleteGroupRequest(int position, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("username", getUsername())
                .add("position", String.valueOf(position))
                .build();

        Request request = new Request.Builder()
                .url(DELETE_GROUP)
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(callback);

    }

    /**
     * 添加好友
     *
     * @param friend   好友账号
     * @param callback 接口回调
     */
    public static void addFriendRequest(String friend, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("username", getUsername())
                .add("friend", friend)
                .build();

        Request request = new Request.Builder()
                .url(ADD_FRIEND)
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(callback);
    }

    /**
     * 删除好友
     *
     * @param friend   好友账号
     * @param callback 接口回调
     */
    public static void deleteFriendRequest(String friend, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("username", getUsername())
                .add("friend", friend)
                .build();

        Request request = new Request.Builder()
                .url(DELETE_FRIEND)
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(callback);
    }


    /**
     * 编辑好友
     *
     * @param friend   好友账号
     * @param key      修改的字段
     * @param value    修改的内容
     * @param callback 接口回调
     */
    public static void editingFriendRequest(String friend, String key, String value, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("username", getUsername())
                .add("friend", friend)
                .add(key, value)
                .build();

        Request request = new Request.Builder()
                .url(EDITING_FRIEND)
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(callback);
    }

    /**
     * 获取好友
     *
     * @param callback 接口回调
     */
    public static void getFriendsRequest(okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("username", getUsername())
                .build();

        Request request = new Request.Builder()
                .url(GET_FRIENDS)
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(callback);
    }

    /**
     * 上传图片
     *
     * @param imagePath 图片路径
     * @param callback  接口回调
     */
    public static void UploadImagesRequest(String imagePath, okhttp3.Callback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("username", getUsername())
                .addFormDataPart("img", getUsername() + ".jpg",
                        RequestBody.create(MEDIA_TYPE_PNG, new File(imagePath)))
                .build();

        Request request = new Request.Builder()
                .header("Authorization", "Client-ID " + "...")
                .url(UPLOAD_IMAGES)
                .post(requestBody)
                .build();

        okHttpClient.newCall(request).enqueue(callback);
    }

    /**
     * @return 返回当前登录用户
     */
    private static String getUsername() {
        SharedPreferences spf = MyApplication.getmContext().getSharedPreferences("user", Context.MODE_PRIVATE);
        return spf.getString("username", "");
    }

}
